package com.sanjiahantang.youjiaclient.module.me.order.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.config.StaticValue;
import com.sanjiahantang.youjiaclient.module.me.order.beans.RequireOrderBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.ServiceListBean;
import com.sanjiahantang.youjiaclient.module.me.order.views.OrderView;
import com.sanjiahantang.youjiaclient.module.me.order.views.impl.ServiceOrderActivity;
import com.sanjiahantang.youjiaclient.module.me.require.views.impl.RequireOrderDetailActivity;

import java.util.ArrayList;
import java.util.List;

import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PARAM;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TYPE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.ORDER_TYPE_CANCEL;
import static com.sanjiahantang.youjiaclient.config.StaticValue.ORDER_TYPE_COMPLETE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.ORDER_TYPE_PART_PAY;
import static com.sanjiahantang.youjiaclient.config.StaticValue.ORDER_TYPE_PAY;

/**
 * Created by zy on 2017/9/27.
 */

public class ServiceOrderAdapter extends RecyclerView.Adapter implements StaticValue{

    private Context context;
    private LayoutInflater inflater;
    //private ServiceListBean  data;

    private List<ServiceListBean.Data> data;
    public ServiceOrderAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public List<ServiceListBean.Data> getData() {
        return data;
    }

    public void setData(List<ServiceListBean.Data> data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrderHolder(inflater.inflate(R.layout.item_me_order,null), data.get(viewType),viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((OrderHolder)holder).bindData(data.get(position),position);
    }
    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return data==null ?0:data.size();
    }

    private class OrderHolder extends RecyclerView.ViewHolder {

        public OrderHolder(View itemView,ServiceListBean.Data  data, int position) {
            super(itemView);
            bindData(data,position);
        }

        private void bindData(ServiceListBean.Data  data, int position) {
                bindGoodsList(itemView,data);
        }
        private void bindGoodsList(View itemView, final ServiceListBean.Data goods) {
            itemView.findViewById(R.id.llAction).setVisibility(View.GONE);
            TextView txtOrderno = (TextView) itemView.findViewById(R.id.txtOrderNum);
            TextView txtOrderStatus = (TextView) itemView.findViewById(R.id.txtCreateTime);
            ListView ServiceOrderListView = (ListView) itemView.findViewById(R.id.goodListView);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.txtGoodsCount);
            TextView txtMoney = (TextView) itemView.findViewById(R.id.txtAmount);
            TextView btn_cancle = (TextView) itemView.findViewById(R.id.txtButton2);
            TextView btn_besure= (TextView) itemView.findViewById(R.id.txtButton);
            TextView txtRemain = (TextView) itemView.findViewById(R.id.txtRemain);
            txtRemain.setVisibility(View.GONE);
            String statusname = null;


            statusname = goods.getStatusName();
            switch (goods.getStatusId()){
                case OrderView.TYPE_UN_PAY://dai
                    statusname = "待付款";
                    btn_cancle.setText("取消订单");
                    btn_besure.setText("去付款");
                    itemView.findViewById(R.id.llAction).setVisibility(View.VISIBLE);
                    btn_cancle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, ServiceOrderActivity.class);
                            intent.putExtra(KEY_TYPE,ORDER_TYPE_CANCEL);
                            Bundle b = new Bundle();
                            b.putSerializable("goods",goods);
                            intent.putExtra(KEY_PARAM,b);
                            context.startActivity(intent);
                        }
                    });


                    btn_besure.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, ServiceOrderActivity.class);
                            intent.putExtra(KEY_TYPE, ORDER_TYPE_PAY);
                            Bundle b = new Bundle();
                            b.putSerializable("goods", goods);
                            intent.putExtra(KEY_PARAM, b);
                            context.startActivity(intent);
                        }
                    });


                    break;
                case OrderView.TYPE_PART_PAY://bufen
                    btn_cancle.setVisibility(View.GONE);
                    btn_besure.setText("去付款");
                    btn_besure.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, ServiceOrderActivity.class);
                            intent.putExtra(KEY_TYPE, ORDER_TYPE_PART_PAY);
                            Bundle b = new Bundle();
                            b.putSerializable("goods", goods);
                            intent.putExtra(KEY_PARAM, b);
                            context.startActivity(intent);
                        }
                    });
                    itemView.findViewById(R.id.llAction).setVisibility(View.VISIBLE);
                    txtRemain.setVisibility(View.VISIBLE);
                    txtRemain.setText(String.format(FORMAT_ORDER_REMAIN,goods.getAreadyResult(),goods.getPayMoney() - goods.getAreadyResult()));
                    break;
                case OrderView.TYPE_PAYED://yifu
                    if (goods.getServiceType() == 3 && goods.getServiceRecordList().get(0).getPlanOrderState() == 1){
                        itemView.findViewById(R.id.llAction).setVisibility(View.VISIBLE);
                        btn_cancle.setVisibility(View.GONE);
                        btn_besure.setText("评价");
                        btn_besure.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(context, ServiceOrderActivity.class);
                                intent.putExtra(KEY_TYPE, ORDER_TYPE_COMPLETE);
                                Bundle b = new Bundle();
                                b.putSerializable("goods", goods);
                                intent.putExtra(KEY_PARAM, b);
                                context.startActivity(intent);
                            }
                        });
                    }else if(goods.getServiceType() != 3){
                        itemView.findViewById(R.id.llAction).setVisibility(View.VISIBLE);
                        btn_cancle.setVisibility(View.GONE);
                        btn_besure.setText("评价");
                        btn_besure.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(context, ServiceOrderActivity.class);
                                intent.putExtra(KEY_TYPE, ORDER_TYPE_COMPLETE);
                                Bundle b = new Bundle();
                                b.putSerializable("goods", goods);
                                intent.putExtra(KEY_PARAM, b);
                                context.startActivity(intent);
                            }
                        });
                    }else{
                        itemView.findViewById(R.id.llAction).setVisibility(View.GONE);
                    }

                    break;
                case OrderView.TYPE_COMPLETE:case OrderView.TYPE_EVALUATED://yiwancheng
                    itemView.findViewById(R.id.llAction).setVisibility(View.GONE);
                    break;
                default:
                    itemView.findViewById(R.id.llAction).setVisibility(View.GONE);
                    statusname = "订单关闭";
                    break;
            }

            int count = 0;//数量
            float price = 0f;
            for(ServiceListBean.Data.ServiceRecordListBean item : goods.getServiceRecordList()){
                count += item.getCount();
                price += Float.parseFloat(String.valueOf(item.getResult()));
            }
            txtMoney.setText(String.format("%.2f",goods.getPayMoney()));
            txtGoodsCount.setText(String.valueOf(goods.getServiceRecordList().size()));
            txtOrderStatus.setText(statusname);
            txtOrderno.setText(goods.getOrderCode()+goods.getApartmentName());
            //txtOrderStatus.setText(goods.get(0).getStatusId());


            final QuickAdapter adapter = new QuickAdapter<ServiceListBean.Data.ServiceRecordListBean>(context,R.layout.item_serviceorderitem) {
                @Override
                protected void convert(BaseAdapterHelper helper, ServiceListBean.Data.ServiceRecordListBean item) {
                    helper.setText(R.id.sg_name,item.getServiceName());
                    helper.setText(R.id.sg_price,String.format("%.2f",item.getResult()));
//                    helper.setText(R.id.sg_count,String.valueOf(item.getCount()));
                    helper.getView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, ServiceOrderActivity.class);
                            Bundle b = new Bundle();
                            b.putSerializable("goods", goods);
                            intent.putExtra(KEY_PARAM, b);
                            context.startActivity(intent);
                        }
                    });
                }
            };

            List<ServiceListBean.Data.ServiceRecordListBean> showList = new ArrayList<>();
            final TextView tvMore = (TextView) itemView.findViewById(R.id.tvMore);
//            if (goods.getServiceRecordList().size() > SHOW_SIZE){
//                showList.clear();
//                showList.addAll(goods.getServiceRecordList().subList(0,SHOW_SIZE));
//                tvMore.setVisibility(View.VISIBLE);
//                tvMore.setTag(R.id.decode,1);
//                tvMore.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        Object tag = view.getTag(R.id.decode);
//                        if (tag != null && tag instanceof Integer){
//                            int type = (int) tag;
//                            if (type == 1){
//                                tvMore.setText("收起列表");
//                                adapter.clear();
//                                adapter.addAll(goods.getServiceRecordList());
//                                tvMore.setTag(R.id.decode,2);
//                            }else{
//                                tvMore.setText("查看全部");
//                                adapter.clear();
//                                adapter.addAll(goods.getServiceRecordList().subList(0,SHOW_SIZE));
//                                tvMore.setTag(R.id.decode,1);
//                            }
//                        }
//                    }
//                });
//            }else{
                showList.clear();
                showList.addAll(goods.getServiceRecordList());
                tvMore.setVisibility(View.GONE);
//            }
            adapter.clear();
            adapter.addAll(showList);
            ServiceOrderListView.setAdapter(adapter);
        }
    }
}
